<?php

namespace backend\controllers;
use Yii;
use yii\web\Controller;
use backend\models\Role;


class RoleController extends Controller
{
	public $layout = 'main1.php';
    public function actionIndex()
    {
        echo '角色管理';
    }
    public function actionAdd()
    {
    	$model = new Role();

    	if($model->load(Yii::$app->request->post())&&$model->validate())
    	{
    		  
    		if($model->save())
    		{
    			$role_id = Yii::$app->db->getLastInsertID();
    			 $data = $_POST;
    			 $where = '';
    			 foreach ($data['node_id'] as $key => $val) {
    			 	$where.=",($role_id,$val) ";
    			 }
    			 $where = substr($where,1);
    			 // echo $where;exit;
    			if(Yii::$app->db->createCommand("insert into seven_role_node values $where")->execute())
    			{
    				$this->redirect(['show']);
    			}
    		}
    		
    	}else
    	{
    		$arr = array();
	    	$data = Yii::$app->db->createCommand('select * from seven_node')->queryAll();
	    	$data = getchild($data);
	    	return $this->render('add',['model'=>$model,'data'=>$data]);
    	}
    	
    }
    public function actionShow()
    {
    	$data = Role::find()->all();
    	return $this->render('show',['data'=>$data]);
    }
    public function actionDelete()
    {
    	$role_id = $_GET['role_id'];
    	if(Role::deleteAll("role_id = $role_id"))
    	{
    		Yii::$app->db->createCommand("delete from seven_role_node where role_id = $role_id")->execute();
    		$this->redirect(['show']);
    	}
    }
    public function actionUpdate()
    {
    	$model = new Role();
    	$role_id = $_GET['role_id'];
    	if($model->load($data = Yii::$app->request->post())&&$model->validate())
    	{
    		$role = Role::findOne($data['role_id']);
    		$role->role_name = $data['Role']['role_name'];
    		$role->role_desc = $data['Role']['role_desc'];
    		$role->role_status = $data['Role']['role_status'];
    		$role->update();
			Yii::$app->db->createCommand("delete from seven_role_node where role_id = ".$data['role_id'])->execute();
			$where = '';
			foreach ($data['node_id'] as $key => $val) {
    			$where.=",($role_id,$val) ";
    		}
    		$where = substr($where,1);
    			 // echo $where;exit;
    		if(Yii::$app->db->createCommand("insert into seven_role_node values $where")->execute())
    		{
    			$this->redirect(['show']);
    		}
    	}
    	else
    	{
    		$role = Role::find();
    		$default = $role->where("role_id = $role_id ")->One();
    		$data = Yii::$app->db->createCommand('select * from seven_node')->queryAll();
    		$arr = array();
    		$role_id = Yii::$app->db->createCommand("select node_id from seven_role_node where role_id = $role_id")->queryAll();
    		foreach ($role_id as $key => $val) {
    			array_push($arr,$val['node_id']);
    		}
		    $data = getchild($data);
		    $model = $default;
		    return $this->render('update',['model'=>$model,'data'=>$data,'role_id'=>$arr]);
    	}
    }
    
}
function getchild($data,$pid=0)
{
		$arr = array();
		foreach ($data as $key => $val) {
			if($val['node_pid'] == $pid)
			{
				$child = getchild($data,$val['node_id']);
				if($child)
				{
					$val['child'] = $child;
				}
				$arr[] = $val;
			}
		}
		return $arr;
}